{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import random\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据读取\n",
    "with open(\"./Prostate_Cancer.csv\",\"r\") as file:\n",
    "    reader = csv.DictReader(file)\n",
    "    datas = [row for row in reader]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据预处理\n",
    "**防止数据之间的一些巧合影响结果，随机打乱一下**\n",
    "\n",
    "*将数据分成三份，一份为测试集，两份为训练集*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分组\n",
    "random.shuffle(datas)\n",
    "n = len(datas)//3\n",
    "\n",
    "test_set = datas[0:n]  # 1/3是测试集\n",
    "train_set = datas[n:]  # 2/3是训练集"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 欧几里得距离\n",
    "\n",
    "\n",
    "*欧几里得度量（euclidean metric）（也称欧氏距离）是一个通常采用的距离定义，指在m维空间中两个点之间的真实距离，或者向量的自然长度（即该点到原点的距离）。在二维和三维空间中的欧氏距离就是两点之间的实际距离。*\n",
    "\n",
    "![欧几里得算法](Euclidean_Distance.jpg \"Euclidean Distance\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 距离\n",
    "def distance(d1,d2): #我们用欧几里得距离\n",
    "    res = 0 # 和\n",
    "\n",
    "    for key in (\"radius\",\"texture\",\"perimeter\",\"area\",\"smoothness\",\"compactness\",\"symmetry\",\"fractal_dimension\"):\n",
    "        res += (float(d1[key]) - float(d2[key]))**2\n",
    "\n",
    "    return res**0.5\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# KNN\n",
    "1. 距离\n",
    "2. 排序——升序\n",
    "3. 取前K个\n",
    "4. 加权平均"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [],
   "source": [
    "# KNN\n",
    "def knn(data,K):\n",
    "    # 1.距离\n",
    "    res = [\n",
    "        {\"result\":train['diagnosis_result'],\"distance\":distance(data,train)}\n",
    "        for train in train_set\n",
    "    ]\n",
    "\n",
    "    # 2.排序——升序\n",
    "    res = sorted(res,key = lambda item:item['distance'])\n",
    "\n",
    "    # 3.取前K个\n",
    "    resK = res[0:K]\n",
    "\n",
    "    # 4.加权平均\n",
    "    result = {'B':0,'M':0}\n",
    "\n",
    "    # 总距离\n",
    "    sum = 0\n",
    "    for r in resK:\n",
    "        sum += r['distance']\n",
    "\n",
    "    for r in resK:\n",
    "        result[r[\"result\"]] += 1 - r[\"distance\"]/sum\n",
    "\n",
    "    if result['B'] > result['M']:\n",
    "        return 'B'\n",
    "    else:\n",
    "        return 'M'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZIAAAEKCAYAAAA4t9PUAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3Xt0nPV95/H3V5eRrZGxNbJkge/YDrZliAnCJJAQIMYSbQO0TVJoaJM0e2h6wtnu0k1JNmnaZUM3gW3paUvbsFuSXpIQIE3rbcEyEEMuBGIDBiwbg2wMFrZs+W7Jtq7f/WOescfySJrRzKMZaT6vc3Q881x+83s80nzn+V2+P3N3RERExqok3xUQEZGJTYFERESyokAiIiJZUSAREZGsKJCIiEhWFEhERCQrCiQiIpIVBRIREcmKAomIiGSlLN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75XeB97v6FdF4waRZzqn1fBq4Cbk6aS1BnZjcMc0o78OV0XleKlwKJTBaPAb9iZhVwOhHfBcBPzazKzJ42s5csvm7LORmfU3xr/2sz+3Tw+DIzezZIitkyZPZz4vj5wWu8Gvw7z8xWAvcCv2TxtS2mpqh3PbAe+Iq7n5NlwczWAlHgBTP7jVSvExz3bTP7czPbAHwj1X+Qmf0B8EvAR939ZNKu+4CvpDoHeAU4ambXD7NfRIFEJgd3Pwj8AmgONt0CfN/jM25PAb8aJLa8FvizIEXGqIJ8TH8FfMzdLwMeAlJlDvhr4muPXAJ8B/hLd98MfDWox8ohH94J/wj8tbs/Osx13QicDM7/fqrXSTr8PcBqd/+DFEVdBXwOuCEp1XjCz4EeM7s2VR2ArzF8oBFRIJFJJbl5K7lZy4A/NbNXgaeIp9GelWaZFwErgCeD9NxfIZ74bqgPAN8NHv8T8ME0y38K+C0zq0zz+JFe51F3HxjmvDbi/w9rhtk/bLBINMkN7eMRSVAgkcnkX4GPmNn7gKmJhYuATwK1wGVBiu19wJQh5/Zz9t9DYr8BrcEdwUp3v9jdh/swTpZu7qF7ieerenSkvo00X6d7hOP2EW/Wuj/VnYe7/4j4Nb9/mPPvQX0lMgwFEpk0giabZ4g3PyV3sk8H9rt7X/AhOj/F6W8Dy4ORTNOJd5IDbAdqzewDEG/qMrOGFOc/x5m7oU8SX+Y0Xf8VOAb8fRpNbmN+HXd/A/g14J+D/puh7gH+cJhz1wPVwHvTfT0pHgokMtl8j/iH3cNJ274DNAbDXD8JvD70JHffTTyL7avB8S8H23uJp+D+hpm9AmwGrkzxuv8Z+EzQfPZbxNfKTkvQj/Mp4HzidygjGfPrBK+1EfgMsNbMFg3Z9zjQOcLp95C6WU+KnLL/iohIVnRHIiIiWVEgERGRrCiQiIhIVhRIREQkKwokIiKSFQUSERHJigKJiIhkRYFERESy8v8B015DD7gG+d4AAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "k_range = range(1, 31)\n",
    "k_error = []\n",
    "for k in k_range:\n",
    "    correct = 0\n",
    "    for test in test_set:\n",
    "        result = test['diagnosis_result']\n",
    "        resultT = knn(test,k)\n",
    "\n",
    "        if result == resultT:\n",
    "            correct += 1\n",
    "    # print(\"K为5时训练结果的准确率为{:.2f}%\".format(100*correct/len(test_set)))\n",
    "    # print(\"K为{:d}时训练结果的准确率为{:.2f}%\".format(k,100*correct/len(test_set)))\n",
    "    k_error.append(1-(correct/len(test_set)))\n",
    "\n",
    "plt.plot(k_range, k_error)\n",
    "plt.xlabel('Value of K for KNN')\n",
    "plt.ylabel('Error')\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a796b201f3e347ad0cb7d4582451e29d2803d0a8d752ae1246d54d048b6d1691"
  },
  "kernelspec": {
   "display_name": "Python 3.8.0 64-bit ('python_pytorch': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
